package com.example.openapp;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;

import java.lang.reflect.Method;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.shutdown)
    Button shutdown;
    @BindView(R.id.reboot)
    Button reboot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //利用服务处理
            Intent intent = new Intent(MainActivity.this, MyAccessibilityService.class);
            intent.putExtra("type", msg.what);
            startService(intent);
        }
    };

    /**
     * 关机
     */
    public void clickShutdonw() {
        if (!isStartAccessibilityService(this, "MyAccessibilityService")) {
            startAccessibilityService();
        } else {
            //关机
            handler.sendEmptyMessageDelayed(MyAccessibilityService.SHUTDOWN_TYPE, 12000);
        }
    }

    /**
     * 重启
     */
    public void clickReboot() {
        if (!isStartAccessibilityService(this, "MyAccessibilityService")) {
            startAccessibilityService();
        } else {
            //重启
            handler.sendEmptyMessageDelayed(MyAccessibilityService.REBOOT_TYPE, 12000);
        }
    }

    /**
     * 前往设置界面开启服务
     */
    private void startAccessibilityService() {
        new AlertDialog.Builder(this)
                .setTitle("开启辅助功能")
                .setIcon(R.mipmap.ic_launcher)
                .setMessage("使用此项功能需要您开启辅助功能")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 隐式调用系统设置界面
                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        startActivity(intent);
                    }
                }).create().show();
    }

    /**
     * 判断AccessibilityService服务是否已经启动
     *
     * @param context
     * @param name
     * @return
     */
    public static boolean isStartAccessibilityService(Context context, String name) {
        AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
        List<AccessibilityServiceInfo> serviceInfos = am.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        for (AccessibilityServiceInfo info : serviceInfos) {
            String id = info.getId();
            if (id.contains(name)) {
                return true;
            }
        }
        return false;
    }

    @OnClick({R.id.shutdown, R.id.reboot})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shutdown:
                clickShutdonw();//关机
                break;
            case R.id.reboot:
                clickReboot();//重启
                break;
        }
    }
}
